package com.chapter.sample.redis;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import redis.clients.jedis.Jedis;

import java.util.*;

public class JedisDemo {
    public static void main(String[] args) {
        //new RedisProperties.Jedis()
        Jedis jedis = new Jedis("172.16.27.149", 6381);
        jedis.auth("123456");

        String ping = jedis.ping();
        System.out.println(ping);

        //key
        Set<String> keys = jedis.keys("*");
        for (Iterator iterator = keys.iterator(); iterator.hasNext(); ) {
            String key = (String) iterator.next();
            System.out.println(key);
        }
        System.out.println("jedis.exists=====>" + jedis.exists("k1"));
        System.out.println("jedis.ttl=====>" + jedis.ttl("k2"));

        System.out.println(jedis.get("k1"));
        jedis.set("k4", "k4_redis");
        jedis.set("k5", "k5_redis");
        System.out.println("------------------------------------------------");
        jedis.mset("str1","v1","str2","v2","str3","v3");
        System.out.println(jedis.mget("str1","str2","str3"));

        System.out.println("------------------------list------------------------");
        jedis.lpush("mylist","v1","v2","v3","v4","v5");
        List<String> list = jedis.lrange("mylist", 0, -1);
        for (String element : list) {
            System.out.println(element);
        }

        System.out.println("------------------------set------------------------");
        jedis.sadd("orders","jd001");
        jedis.sadd("orders","jd002");
        jedis.sadd("orders","jd003");
        Set<String> set1 = jedis.smembers("orders");
        set1.forEach(s ->{
            System.out.println(s);
        });
        System.out.println(jedis.sismember("orders", "jd002"));
        jedis.srem("orders","jd002");
        System.out.println(jedis.sismember("orders", "jd002"));
        System.out.println(jedis.smembers("orders").size());

        System.out.println("------------------------hash------------------------");
        jedis.hset("hash1","userName","lisi");
        System.out.println(jedis.hget("hash1","userName"));
        Map<String,String> map = new HashMap<>();
        map.put("telphone","138xxxxxxxx");
        map.put("address","beijing");
        map.put("email","guangzhou@164.com");
        jedis.hmset("hash1",map);
        List<String> hmget = jedis.hmget("hash1", "userName", "telphone", "address", "email");
        for (String element : hmget) {
            System.out.println(element);
        }

        System.out.println("------------------------zset------------------------");
        jedis.zadd("zset01",60D,"v1");
        jedis.zadd("zset01",70D,"v2");
        jedis.zadd("zset01",80D,"v3");
        jedis.zadd("zset01",90D,"v4");
        //jedis.zcard("zset01","v1");
        List<String> zset01 = jedis.zrange("zset01", 0, -1);
        zset01.forEach(System.out::println);


    }
}
